package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysDataSync;
import com.ruoyi.system.service.ISysDataSyncService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 信息同步Controller
 * 
 * @author wuyafeng
 * @date 2025-01-19
 */
@Controller
@RequestMapping("/system/sync")
public class SysDataSyncController extends BaseController
{
    private String prefix = "system/sync";

    @Autowired
    private ISysDataSyncService sysDataSyncService;

    @RequiresPermissions("system:sync:view")
    @GetMapping()
    public String sync()
    {
        return prefix + "/sync";
    }

    /**
     * 查询信息同步列表
     */
    @RequiresPermissions("system:sync:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysDataSync sysDataSync)
    {
        startPage();
        List<SysDataSync> list = sysDataSyncService.selectSysDataSyncList(sysDataSync);
        return getDataTable(list);
    }

    /**
     * 导出信息同步列表
     */
    @RequiresPermissions("system:sync:export")
    @Log(title = "信息同步", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysDataSync sysDataSync)
    {
        List<SysDataSync> list = sysDataSyncService.selectSysDataSyncList(sysDataSync);
        ExcelUtil<SysDataSync> util = new ExcelUtil<SysDataSync>(SysDataSync.class);
        return util.exportExcel(list, "信息同步数据");
    }

    /**
     * 新增信息同步
     */
    @RequiresPermissions("system:sync:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存信息同步
     */
    @RequiresPermissions("system:sync:add")
    @Log(title = "信息同步", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysDataSync sysDataSync)
    {
        return toAjax(sysDataSyncService.insertSysDataSync(sysDataSync));
    }

    /**
     * 修改信息同步
     */
    @RequiresPermissions("system:sync:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        SysDataSync sysDataSync = sysDataSyncService.selectSysDataSyncById(id);
        mmap.put("sysDataSync", sysDataSync);
        return prefix + "/edit";
    }

    /**
     * 上传信息
     */
    @RequiresPermissions("system:sync:uploadData")
    @Log(title = "上传信息", businessType = BusinessType.UPDATE)
    @GetMapping("/uploadData/{id}")
    @ResponseBody
    public AjaxResult uploadData(@PathVariable("id") Integer id)
    {
        SysDataSync sysDataSync = sysDataSyncService.selectSysDataSyncById(id);
        logger.info("开始向【"+ sysDataSync.getProjectName()+"】项目上传数据");
        sysDataSync.setUpdateTime(new Date());
        if( sysDataSyncService.uploadDataSync(sysDataSync)){//执行上传功能
            sysDataSyncService.updateSysDataSync(sysDataSync);
        }
        return AjaxResult.success( "开始向【"+sysDataSync.getProjectName()+"】项目上传数据。。。");
    }


    /**
     * 修改保存信息同步
     */
    @RequiresPermissions("system:sync:edit")
    @Log(title = "信息同步", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysDataSync sysDataSync)
    {
        return toAjax(sysDataSyncService.updateSysDataSync(sysDataSync));
    }

    /**
     * 删除信息同步
     */
    @RequiresPermissions("system:sync:remove")
    @Log(title = "信息同步", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysDataSyncService.deleteSysDataSyncByIds(ids));
    }
}
